<?php

declare(strict_types=1);

namespace app\backend\controller\crm;

use app\common\controller\Basic;
use app\common\service\crm\RecordService;
use app\common\service\hrm\FilesService;
use app\common\service\sys\UserService;

/**
 * 客户跟进记录 控制器
 * @author 吕玉冰
 * @date  2022-07-20
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class ContractRecord extends Basic
{

    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new RecordService();
    }

    /**
     * 列表
     */
    public function index()
    {
        if ($this->request->isGet()) {
            $data = input('param.');
            $data['relation_type'] = 3;
            return json($this->service->_list($data));
        }
        return _jerr(1000);
    }

    /**
     * 添加 关联类型  1客户 2联系人 3合同 4线索
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data = input('param.');
            $this->_form_filter($data);
            $data['create_user_id'] = $this->user['id'];
            $data['create_user_name'] = $this->user['real_name'];
            $data['owner_user_id'] = $this->user['id'];
            $data['owner_user_name'] = $this->user['real_name'];
            $data['relation_type'] = 3;
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = input('param.');
            return _jok('ok', [
                'einfo' => (new UserService())->getEmployeeInfo($this->user['id']),
                'fi' => [],
            ]);
        }
        return _jerr(1000);
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $id = input('id/d', 0);
        if ($id === 0) {
            return _jerr(1022);
        }
        if ($this->request->isPost()) {
            $data = input('param.');
            $data['create_user_id'] = $this->user['id'];
            $data['create_user_name'] = $this->user['real_name'];
            $data['owner_user_id'] = $this->user['id'];
            $data['owner_user_name'] = $this->user['real_name'];
            $data['relation_type'] = 3;
            $this->_form_filter($data);
            return json($this->service->saveForm($data));
        }
        if ($this->request->isGet()) {
            $data = $this->service->getInfoById($id);
            $data['fi'] = FilesService::getFilesJson([
                'id' => $id,
                'table_name' => 'crm_record'
            ]);
            return _jok('ok', $data);
        }
        return _jerr(1000);
    }

    /**
     * 添加编辑-表单数据处理
     */
    protected function _form_filter(&$data)
    {
        $data = rq_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isPost()) {
            $id = input('id', '');
            if ($id === 0) {
                return _jerr(1002);
            }
            return json($this->service->_del($id));
        }
        return _jerr(1000);
    }
}
